//1.手机注册和邮箱注册之间的切换
mail.onclick = function () {
    tab2.style.display = "block";
    tab1.style.display = "none"
    mail.style.background = "white"
    iphone.style.background = "#ccc"
}
iphone.onclick = function () {
    tab1.style.display = "block";
    tab2.style.display = "none"
    mail.style.background = "#ccc"
    iphone.style.background = "white";
}

var regbtn = document.getElementById("regbtn");

regbtn.onclick = function(){
    var telValue = tel.value;
    var pwdValue = pwd.value;
    var yzmValue = yzm.value;

    var telIsRight = false;
    var pwdIsRight = false;

    var telReg = /^1[3-9][0-9]{9}$/;

    if(telValue.length==0){
        error1.innerHTML = "此项必须填写";
        telIsRight = false;
    }else if(!telReg.test(telValue)){
        error1.innerHTML = "手机号无效";
        telIsRight = false;
    }else{
        telIsRight = true;
    }

    if(pwdValue.length==0){
        error2.innerHTML = "此项必须填写";
        error2.className = "error";
        pwd.className = "";
        pwdIsRight = false;
    }else if(pwdValue.length<6){
        error2.innerHTML = "密码至少6位，支持数字、字母和符号";
        error2.className = "error";
        pwd.className = "";
        pwdIsRight = false;
    }else if(pwdValue.length<8){
        error2.innerHTML = "强度高的密码至少需要8个字符，支持数字、字母和符号";
        error2.className = "warn";
        pwd.className = "warnbg";
        pwdIsRight = true;
    }else{
        error2.innerHTML = "";
        error2.className = "error";
        pwd.className = "";
        pwdIsRight = true;
    }

    if(yzmValue.length==0){
        error3.innerHTML = "此项必须填写";
    }else{
        $.ajax({
            type:"POST",
            url:baseurl+"/bijiyzm/find2",
            contentType:"application/json; charset+utf-8",
            data:JSON.stringify({
                tel:tel.value,
                yzm:yzm.value
            }),
            success:function(data){
                if(!data){
                    error3.innerHTML = "验证码错误";
                }else{
                    if(telIsRight&&pwdIsRight){
                        $.ajax({
                            type:"POST",
                            url:baseurl+"/bijiuser/add",
                            contentType:"application/json; charset+utf-8",
                            data:JSON.stringify({
                                "createDate": new Date().toLocaleString(),
                                "pwd": pwd.value,
                                "tel": tel.value
                            }),
                            success:function(data){
                                if(data){
                                    error3.innerHTML = "注册成功";
                                }
                            },
                            error:function(data){
                                tip.innerHTML = "发生错误，请检查网络";
                            }
                        })
                    }
                };
            },
        })
    }
};

tel.onfocus = function(){
    error1.innerHTML = "";
};

tel.onblur = function(){
    var telReg = /^1[3-9][0-9]{9}$/;
    var telValue = tel.value;

    if(telValue.length==0){
        error1.innerHTML = "此项必须填写";
        return;
    }else if(!telReg.test(telValue)){
        error1.innerHTML = "手机号无效";
        return;
    }

    $.ajax({
        type:"POST",
        url:baseurl+"/bijiuser/checktel",
        contentType:"application/json;charset=utf-8",
        data:JSON.stringify({
            "tel":telValue
        }),
        success:function(data){
            error1.innerHTML = data?"":"手机号已经注册！";
        }
    })
}

pwd.onfocus = function(){
    error2.innerHTML = "";
};

yzm.onfocus = function(){
    error3.innerHTML = "";
};


getyzmbtn.onclick = function(){
    var telReg = /^1[3-9][0-9]{9}$/;
    var telValue = tel.value;

    if(telValue.length==0){
        error1.innerHTML = "此项必须填写";
        return;
    }else if(!telReg.test(telValue)){
        error1.innerHTML = "手机号无效";
        return;
    }

    error1.innerHTML = "";

    sengMsg(tel.value,false);
};

    //1. 当单击注册按钮时，判断两个文本框的值是否合法，如果合法提交给服务器处理
    regbtn2.onclick = function () {
        var emailValue = email.value;
        var regReg = /^\w+@\w+(\.[a-zA-Z]{2,4}){1,2}$/; //不必要理解
        var emailRight = false;
        var pwd1Right = false;

        if (emailValue.length == 0) {
            e1.innerHTML = "此项必填";
            emailRight = false;
        }else if(!regReg.test(emailValue)){
            e1.innerHTML = "邮箱号无效";
            emailRight = false;
        }else{
            e1.innerHTML = "";
            emailRight = true;
        }

        var pwd1Value = pwd1.value;
        var isWarn = false;

        if (pwd1Value.length == 0) {
            e2.innerHTML = "此项必填";
            pwd1Right = false;
        }else if (pwd1Value.length < 6) {
            e2.innerHTML = "密码至少6位，支持数字、字母和符号";
            pwd1Right = false;
        }else if(pwd1Value.length<8){
            e2.innerHTML = "强度高的密码至少需要8个字符，同时包含大小写字母、数字和符号";
            e2.className = "warn";
            pwd1.className = "warnbg";
            isWarn = true;
            pwd1Right = true;
        }else{
            e2.innerHTML = "";
            pwd1Right = true;
        }

        if(!isWarn){
            e2.className = "error";
            pwd1.className = "";
        }

        //验证是否要给服务器发请求 1拦2延  拦的是不合法的数据，延迟对服务器请求
        if(emailRight&&pwd1Right){
            ajax("/bijiuser/checktel",{tel:emailValue},function(data){
               if(data){
                   // 注册
                  ajax("/bijiuser/add",{tel:emailValue,pwd1:pwd1Value,accountType:1},function(data){
                      if(data){
                          tip.innerHTML = "注册成功！已经发送邮件到您的邮箱，请到邮箱激活您的帐号！";
                          setTimeout(function(){
                              location = "http://mail.qq.com";
                          },2000);
                      }else{
                          tip.innerHTML = "注册失败！";
                      }
                  }) ;
               }else{
                   e1.innerHTML = "此邮箱号已经注册！";
               }
            });
        }
    };

    //2. 当光标进入文本框时，消除对该文本框的错误提示
    email.onfocus = function(){
      e1.innerHTML = "";
    };

    pwd1.onfocus = function(){
      e2.innerHTML = "";
    };

    //3. 关于密码框实时判断
    pwd.onkeyup = function(){
        var pwd1Value = pwd1.value;

        var isWarn = false;

        if (pwd1Value.length == 0) {
            e2.innerHTML = "此项必填";
        }else if (pwd1Value.length < 6) {
            e2.innerHTML = "密码至少6位，支持数字、字母和符号";
        }else if(pwd1Value.length<8){
            e2.innerHTML = "强度高的密码至少需要8个字符，同时包含大小写字母、数字和符号";
            e2.className = "warn";
            pwd1.className = "warnbg";
            isWarn = true;
        }else{
            e2.innerHTML = "";
        }

        if(!isWarn){
            e2.className = "error";
            pwd1.className = "";
        }

    }